//
//  HDXMangerTool.m
//  ProjectforHDX
//
//  Created by H.DX on 16/4/1.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "HDXMangerTool.h"

#import <sys/socket.h>
#import <sys/sockio.h>
#import <sys/ioctl.h>
#import <net/if.h>
#import <arpa/inet.h>

@implementation HDXMangerTool

/**
 *  获取版本号
 */
+(NSString*)getVersion{
    
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    
    //    NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];
    
    
    return [NSString stringWithFormat:@"%@",app_Version];
}
/**
 *   检测用户是否推送了版本更新 首先要有推送 其次推送的版本高于用户当前版本 返回AppStore的路径
 */
+ (NSDictionary*)getJpushVersion{
    
    //查找系统推送记录
    NSDictionary *sqlda=[FMDataSqlManger selectTable:@"et_message" where:[NSDictionary dictionaryWithObjectsAndKeys:@"0",@"type",nil]];
    if([sqlda allKeys].count<1){
        return nil;
    }
    else{
        NSDictionary *versionDict=[UtilCheck dictionaryWithJsonString:[sqlda objectForKey:@"content"]];
        
        if([[versionDict allKeys] containsObject:@"Version"]){
            
            //当前版本小于服务器版本为真
            if ([[versionDict objectForKey:@"Version"] compare:[self getVersion] options:NSNumericSearch] == NSOrderedDescending){
                //版本高于当前版本
                return versionDict;
            }
            else{
                //版本不高于当前版本
                [FMDataSqlManger deleteTable:@"et_message" condition:@"type=0"];
                return nil;
            }
            
        }
        else{
            //版本不高于当前版本
            [FMDataSqlManger deleteTable:@"et_message" condition:@"type=0"];
            
            return nil;
        }
        
    }
}


/**
 * 获取本地IP
 */
+(NSString*)getIphoneIpAddress{
    
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    
    if (sockfd < 0) return nil;
    
    NSMutableArray *ips = [NSMutableArray array];
    
    
    
    int BUFFERSIZE = 4096;
    
    struct ifconf ifc;
    
    char buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;
    
    struct ifreq *ifr, ifrcopy;
    
    ifc.ifc_len = BUFFERSIZE;
    
    ifc.ifc_buf = buffer;
    
    if (ioctl(sockfd, SIOCGIFCONF, &ifc) >= 0){
        
        for (ptr = buffer; ptr < buffer + ifc.ifc_len; ){
            
            ifr = (struct ifreq *)ptr;
            
            int len = sizeof(struct sockaddr);
            
            if (ifr->ifr_addr.sa_len > len) {
                
                len = ifr->ifr_addr.sa_len;
                
            }
            
            ptr += sizeof(ifr->ifr_name) + len;
            
            if (ifr->ifr_addr.sa_family != AF_INET) continue;
            
            if ((cptr = (char *)strchr(ifr->ifr_name, ':')) != NULL) *cptr = 0;
            
            if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0) continue;
            
            memcpy(lastname, ifr->ifr_name, IFNAMSIZ);
            
            ifrcopy = *ifr;
            
            ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy);
            
            if ((ifrcopy.ifr_flags & IFF_UP) == 0) continue;
            
            
            
            NSString *ip = [NSString  stringWithFormat:@"%s", inet_ntoa(((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr)];
            
            [ips addObject:ip];
            
        }
        
    }
    
    close(sockfd);
    
    
    
    
    
    NSString *deviceIP = @"";
    
    for (int i=0; i < ips.count; i++)
        
    {
        
        if (ips.count > 0)
            
        {
            
            deviceIP = [NSString stringWithFormat:@"%@",ips.lastObject];
            
            
            
        }
        
    }
    
    return deviceIP;
}

@end
